import { storeToRefs } from 'pinia'
import { useAccountStore } from '@/store/account'
import { TAB_NAMES as HOME_TAB_NAMES, TAB_TYPE as HOME_TAB_TYPE } from '@/constant/home'
import { TAB_NAMES as SEARCH_TAB_NAMES, TAB_TYPE as SEARCH_TAB_TYPE } from '@/constant/search'

export function favoriteAuthCheck(to) {
  if (to.name === 'lang-search' && to.params.tab === SEARCH_TAB_NAMES[SEARCH_TAB_TYPE.FAVORITE]) {
    const { isLogin } = storeToRefs(useAccountStore())
    if (!isLogin.value) {
      return { name: 'search', params: { tab: SEARCH_TAB_NAMES[SEARCH_TAB_TYPE.SEARCH] } }
    }
  }
}

/** 如果不是從 prevName 的路由跳轉過來的，則轉址到 fallbackRoute 的路由 */
export function redirectIfNotPrev({
  from,
  next,
  prevName,
  fallbackRoute = { name: 'home', params: { tab: HOME_TAB_NAMES[HOME_TAB_TYPE.FORYOU] } },
}) {
  return !from.name || !from.name.includes(prevName) ? next(fallbackRoute) : next()
}
